<?php
if (!defined('__COLA_DIR__')) exit;
/**
 * 文章管理列表组件
 *
 * @author edikud
 * @package Widget
 * @copyright Copyright (c) 2019 Cola (http://www.mcooo.com)
 * @license GNU General Public License 2.0
 */
class Widget_Contents_Post_Ucenter extends Widget_Abstract_Contents
{
    /**
     * 内容个数
     *
     * @access private
     * @var integer
     */
    protected $_total = false;

    /**
     * 当前页
     *
     * @access private
     * @var integer
     */
    protected $_currentPage = 1;
	
    /**
     * 执行函数
     *
     * @access public
     * @return void
     */
    public function execute()
    {

        $this->parameter->setDefault('pageSize=20');
        $this->_currentPage = $this->request->get('page', 1);

        /** 构建基础查询 */
        $select = $this->select();
		
		/** 按post查询 */
        $select->where('table.contents.type = ?', 'post');
		
        /** 如果具有编辑以上权限,可以查看所有文章,反之只能查看自己的文章 */
        if (!$this->user->pass('editor', true)) {
            $select->where('table.contents.uid = ?', $this->user->uid);
        } else {
			
			if (isset($this->request->uid)) {
                $select->where('table.contents.uid = ?', $this->request->filter('int')->uid);
			} else if('off' == $this->request->all){
                $select->where('table.contents.uid = ?', $this->user->uid);
			}
		}	

        /** 过滤分类 */
        if (NULL != ($category = $this->request->category)) {
			$select->join('table.relate', 'table.contents.cid = table.relate.cid')
            ->where('table.relate.mid = ?', $category);
        }

        /** 过滤标题 */
        if (NULL != ($keywords = $this->request->filter('search')->keywords)) {
            $select->where('table.contents.title LIKE ?', '%' . $keywords . '%');
        }

		/** 计算数目 */
		$this->_total = (clone $select)
            ->select(array('COUNT(*)' => 'num'))
            ->fetchObject()->num;

        /** 提交查询 */
        $select->order('table.contents.cid', 'DESC')
        ->page($this->_currentPage, $this->parameter->pageSize);

        $select->fetchAll(array($this, 'push'));
    }

    /**
     * 输出分页
     *
     * @access public
     * @return void
     */
    public function pageNav()
    {
        $query = $this->request->makeUriByRequest('page={page}');

        /** 使用盒状分页 */
        $nav = new Page_Box($this->_total,
        $this->_currentPage, $this->parameter->pageSize, $query);
        return $nav->render('&laquo;', '&raquo;');
    }
	
    public function postList()
    {
		$this->widget('Widget_Metas_Category_List')->to($category);

        $html ='
		<form method="get">
		    <div class="cola-search">
                <select name="category">
                <option value="">'._t('分类').'</option>';
                while($category->next()){
                    $html .= '<option value="'.$category->mid.'" '.($this->request->get('category') == $category->mid ? 'selected="true"' : '') .' >'. $category->name.'</option>';
			    }
        $html .= '</select> ';		
			
		if($this->user->pass('editor', true) && !isset($this->request->uid)){	
            $html .= '<select name="all">
                <option value="on"'.((null === $this->request->all || 'on' == $this->request->all) ? ' selected="true"' : '') .'>'._t('所有').'</option>
                <option value="off"'.('off' == $this->request->all ? ' selected="true"' : '') .'>'._t('我的').'</option>';
            $html .= '</select>';
		}
		
        $html .= '<input type="text" placeholder="'._t('请输入关键字').'" value="'. htmlspecialchars($this->request->keywords).'" name="keywords" />';
		
        if ('' != $this->request->keywords || '' != $this->request->category || '' != $this->request->uid || '' != $this->request->all){
            $html .= '<a class="cola-search-cancel ci-cancel" href="'.$this->options->ucenterUrl('posts', false).'"></a>';
	    }
        if(isset($this->request->uid)){
            $html .= '<input type="hidden" value="'.htmlspecialchars($this->request->uid).'" name="uid" />';
        }
        $html .= '</div></form>';

        if ($this->have()) {
			$WidgetMenu = $this->widget('Widget_Menu');
			$categories = $this->widget('Widget_Metas_Category_List');
			$Layout = new Layout();
			$Layout->header(array(
				$this->widget('Widget_Contents_Post_Edit')->operate(),

			    _t('标题'),
				_t('分类'),
				_t('作者'),
				_t('日期'),

			));
			while ($this->next()){
				$mids = explode(',', $this->mids);
				$length = count($mids);
				$cat = '';
                foreach ($mids as $mid){
					--$length;
					$category = $categories->getCategory($mid);
                    $category && $cat .= '<a href="'.$this->request->makeUriByRequest('page=1&category=' . $category['mid']) .'">' . $category['name'] . '</a>' . ($length ? ', ' : '');
                }

			    $Layout->lists(array(
				    '<a class="cola-balloon" href="'.$this->options->ucenterUrl('comments?cid=' . $this->cid, false).'">'.$this->commentsNum.'</a>&nbsp;&nbsp;<a href="'.$this->options->ucenterUrl('post?cid=' . $this->cid, false).'">'.$this->title.'</a>&nbsp;&nbsp;<a class="text-muted text-md" href="'.$this->permalink.'"><i class="ci-link"></i></a>',
					$cat,
					'<a href="'.$this->request->makeUriByRequest('uid=' . $this->uid).'">'.$this->author->name.'</a>',
					'<span class="text-muted text-sm">' . $this->dateWord . '</span>'
				), array('data-cid'=>$this->cid));
			}
			$Layout->append('<ul class="cola-pager">' . $this->pageNav() . '</ul>');
			$html .= $Layout->render();
		}

		return $html;
	}

}